import Http from "../utils/Http";
import Crypt from "../utils/Crypt";

export default class PayModel extends Http {

  getPayAmount(data) {
    return this.request({
      url: '/h5/order/get-amount',
      data
    })
  }

  _getOpenid(data) {
    return this.request({
      url: '/h5/mini/user/get-openid',
      data
    })
  }

  thirdMini(data) {
    return this.request({
      url: '/pay/pay/mini',
      data
    })
  }

  // 这个是汇聚
  payPrepare(params) {
    return new Promise(resolve => {
      this._getOpenid({}).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (!data.openid || data.openid.length < 1) {
          wx.navigateTo({
            url: '/sub_main/pages/user/login/index'
          })
        } else {
          this.request({
            url: '/pay/pay/prepare',
            data: {
              openid: data.openid,
              ...params
            }
          }).then(response => {
            resolve(response.data)
          })
        }
      })
    })
  }

  // 这个是微信
  payPrepareWx(params) {
    return new Promise(resolve => {
      this._getOpenid({}).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (!data.openid || data.openid.length < 1) {
          wx.navigateTo({
            url: '/sub_main/pages/user/login/index'
          })
        } else {
          this.request({
            // url: '/pay/pay-hj/prepare',
            url: '/pay/pay-wx/prepare',
            data: {
              openid: data.openid,
              ...params
            }
          }).then(response => {
            resolve(response.data)
          })
        }
      })
    })
  }

}
